<html>
<head>
<meta http-equiv="cache-control" content="no-cache, must-revalidate"/>
<meta http-equiv="Expires" Content="Mon, 25 May 2009 19:07:03 GMT">
<script type="text/javascript" src="../../src/gew.js"></script>

<script type="text/javascript">
var testScreenIdx=0;
var elapseMls = 0;
var frameCount = 0;
function MyScreen(game)
{
    gewScreen.call(this,game);
    this.scene3d = new gewScene3D(this.game);
    this.onTick = function(mls)
    {   
	  var gl = this.game.gl;
      gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
      this.scene3d.render();
	  var root = this.scene3d.root;
      elapseMls+=mls;
      frameCount++;
	  var old_pos = this.kni.getPosition();
	  var new_pos = new gewVec3(old_pos.x+0.01, old_pos.y+0.01,old_pos.z);
	  var angle = this.kni.getRotateAngle();
	  angle+= mls*(0.05);
	  this.kni.setRotateXYZ(0.,0.,1.,angle);
	  this.kni.setPosition(new_pos);
      if(elapseMls>=2000)
      {
        var fps = frameCount/2;
        document.getElementById("fps").innerHTML="Frame Rate:"+fps;
        elapseMls = 0;
        frameCount = 0;
	  }              
    }
    this.init = function()
    {
	   var game = this.game;
	   var gl = this.game.gl;
	   var scene3d  = this.scene3d;
	   var root = scene3d.root; 
	   root.debugName = "black_screen_scene3d_root"; 
	   var camera_system = scene3d.camera_system;
       // init camera
       var cam = camera_system.createCamera(gewWorldCamera, null, root);
       cam.setDesiredPos(new gewVec3(10.0,10.0,10.0));
       cam.setPitch(-gewCONST_VALUES.PI/4);
       cam.setFov(60.0);
       cam.setAspectRatio(1.0);
       cam.setNear(0.1);
       cam.setFar(100);
	   cam.debugName = "camera";
       camera_system.setActiveCamera(cam);
	 
	   var shadow_effect = new gewCSMEffect(game,512,512);
	   var effect = new gewPhongEffect(game);
	   scene3d.addEffect(shadow_effect);
	   scene3d.addEffect(effect);
	   var shadow_material = new gewCSMMaterial(shadow_effect);
	   var wood = new gewPhongMaterial(effect);
	   var stone = new gewPhongMaterial(effect);
	   wood.shininess = 10;
	   wood.specular.set(0.2,0.2,0.2);
	   
	   stone.shininess = 100;
	   stone.specular.set(1.,0.,0.);
	   stone.shadowMap = shadow_effect.shadowMap;
	   
	   var tmanager = this.game.textureManager;
	   var m_manager = this.game.meshManager;
       tmanager.loadTexture("stone","Resource/stone.jpg");
	   tmanager.loadTexture("wood","Resource/wood.JPG");
	   m_manager.loadISM1("knight","Resource/knight.ism1");
	   //m_manager.loadObj("knight","Resource/knight.obj");
	   //m_manager.loadObj("table","Resource/table_tes.obj");
	   m_manager.loadISM1("table","Resource/table_tes.ism1");
	   
	   //csm_material.texture = tmanager.getTexture("stone");
	   stone.texture = tmanager.getTexture("stone");  
	   wood.texture = tmanager.getTexture("wood"); 
	   stone.shadowMap = shadow_effect.shadowMap;
	   wood.shadowMap = shadow_effect.shadowMap;
	   var knight_mesh = m_manager.getMesh("knight");
	   var table_mesh = m_manager.getMesh("table");
	   
	   var node = new gewTestNode();
	   node.debugName = "node knight 1";
	   var node2 = new gewTestNode();
	   node2.debugName = "node knight 2";
	   var tableNode = new gewTestNode();
	   tableNode.debugName = "table node";
	   tableNode.setMesh(table_mesh);
	   tableNode.addMaterial(shadow_material);
	   tableNode.addMaterial(wood);
	   tableNode.setPositionXYZ(0,0,-5);
	   tableNode.setScaleXYZ(2,2,2);
	   tableNode.setRotateXYZ(1,0,0,90);
	   
	   
	   node.setMesh(knight_mesh);
	   node.addMaterial(shadow_material);
	   node.addMaterial(stone);
	   node.setPositionXYZ(0,0,0);
	   node.setScaleXYZ(1,1,1);
	   root.appendChild(node);
	   root.appendChild(tableNode);
	   
	   node2.setMesh(knight_mesh);
	   node2.addMaterial(shadow_material);
	   node2.addMaterial(stone);
	   node2.setPositionXYZ(0,0,0);
	   node2.setScaleXYZ(1,1,1);
	   root.appendChild(node2);
	   
	   
	   this.kni = node;
    }
    this.isDrag = false;
    this.onMouseDown = function(e)
    {
        this.isDrag = true;
    }
    this.onMouseMove = function(e)
    {
        if( this.isDrag)
            console.log("move move");
    }
    this.onMouseUp = function(e)
    {
        this.isDrag = false;
    }
}
function main()
{
    var game = new gewGame("cavans_id");
    var blackScreen = new MyScreen(game);
    game.addScreen(blackScreen,testScreenIdx);
    game.setScreen(testScreenIdx);
    blackScreen.init();
    setInterval(function(){game.run()},10);
}
</script>
</head>
<body onload="main()">
  <div style="text-align: center">
    <!-- this is the canvas that we render to -->
    <canvas id="cavans_id" width="640" height="480"></canvas>
    <!-- a convenient div to use to display log messages -->
    <div id="fps"></div>
    <a>Dont see anything? Get FireFox 3.7 and enable webGL feature ( about:config >> set webGL.. param to true )</a>
  </div>
</body>
</html>                  